using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cleanButton.Enabled = false;
            uninstallButton.Enabled = false;
        }



        private void cleanButton_Click(object sender, EventArgs e)
        {
            try
            {
                File.Delete(listBox1.Text);
                if (this.listBox1.SelectedIndex >= 0) 
                    this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
            }
            catch (ArgumentException IOex)
            {

            }
        }

        private void tempFilesButton_Click_1(object sender, EventArgs e)
        {
            uninstallButton.Enabled = false;
            listBox1.Items.Clear();
            string sourcePath = @"C:\Windows\temp\";
            this.DirSearch(sourcePath);
            
        }

        private void DirSearch(string sDir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(sDir))
                {
                    listBox1.Items.Add(f);
                }

                foreach (string d in Directory.GetDirectories(sDir))
                {
                    this.DirSearch(d);
                }
            }
            catch (System.Exception excpt)
            {
                listBox1.Items.Add(excpt.Message);
            }
        }

        private void uninstallButton_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            uninstallButton.Enabled = true;
            cleanButton.Enabled = true;
            Installed();
        }
        private string Installed()
        {
            string Software = null;

            string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
            {
                
                foreach (string skName in rk.GetSubKeyNames())
                {
                    using (RegistryKey sk = rk.OpenSubKey(skName))
                    {
                        try
                        {
                        
                            if (!(sk.GetValue("DisplayName") == null))
                            {
                             
                                if (sk.GetValue("InstallLocation") == null)
                                    Software += sk.GetValue("DisplayName") + " - Install path not known\n"; 
                                else
                                    Software += sk.GetValue("DisplayName") + " - " + sk.GetValue("InstallLocation") + "\n"; 
                            }
                        }
                        catch (Exception ex)
                        {
                            
                        }
                    }
                }
            }

            return Software;
        }
    }
}
